﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using eeenno.ReaderEngine;
using eeenno.ReaderEngine.Utilities;

namespace eeenno.SpeeedReaderUI.Forms
{
    public partial class SettingsCacheForm : Form
    {
        private delegate void _toggleWaitCursorCallback(bool enable);

        public SettingsCacheForm()
        {
            InitializeComponent();
            LoadSettings();
            label_cache_size.Text = SpeeedReader.CacheSize();
            if (Settings.caching_enabled)
            {
                label_cache_loc.Text = Settings.cache_path;
            }
        }

        private void LoadSettings()
        {
            if (!Settings.caching_enabled) comboBox_location.SelectedIndex = 0;
            else if (Settings.store_cache_on_memory_card) comboBox_location.SelectedIndex = 2;
            else comboBox_location.SelectedIndex = 1;

            comboBox_articles.Text = Settings.max_articles_to_cache + " Articles";  
            
        }

        private void menuItem_save_Click(object sender, EventArgs e)
        {
            switch (comboBox_location.SelectedIndex)
            {
                case 0:
                    Settings.caching_enabled = false;
                    break;
                case 1:
                    Settings.caching_enabled = true;
                    Settings.store_cache_on_memory_card = false;
                    break;
                case 2:
                    Settings.caching_enabled = true;
                    Settings.store_cache_on_memory_card = true;
                    break;
            }

            Settings.max_articles_to_cache = int.Parse(comboBox_articles.Text.Split(' ')[0]);  

            Settings.WriteSettings();
            DialogResult = DialogResult.OK;
        }

        private void menuItem_cancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void button_clear_cache_Click(object sender, EventArgs e)
        {
            ThreadFactory.DoWork(ClearCache);            
        }

        private void ClearCache(object stateinfo)
        {
            ToggleWaitCursorCallback(true);
            if (SpeeedReader.EmptyCache())
            {
                MainForm.DisplayMessageBox("Cache cleared");
            }
            else
            {
                MainForm.DisplayMessageBox("Error occured while clearing the cache");
            }
            ToggleWaitCursorCallback(false);
        }

        private void ToggleWaitCursorCallback(bool enable)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new _toggleWaitCursorCallback(ToggleWaitCursorCallback), enable);
            }
            else
            {
                if (enable)
                {
                    Cursor.Current = Cursors.WaitCursor;
                }
                else
                {
                    Cursor.Current = Cursors.Default;
                    label_cache_size.Text = SpeeedReader.CacheSize();
                }
            }
        }

        private void comboBox_location_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (comboBox_location.SelectedIndex)
            {
                case 0:
                    Settings.caching_enabled = false;
                    break;
                case 1:
                    Settings.caching_enabled = true;
                    Settings.store_cache_on_memory_card = false;
                    break;
                case 2:
                    Settings.caching_enabled = true;
                    Settings.store_cache_on_memory_card = true;
                    break;
            }
            if (Settings.caching_enabled)
            {
                label_cache_loc.Text = Settings.cache_path;
            }
            else
            {
                label_cache_loc.Text = String.Empty;
            }
            Settings.WriteSettings();
            label_cache_size.Text = SpeeedReader.CacheSize();
        }
    }
}